from common.unit import AndroidUI
from common.home_page import HomePageObject
from common.qr_code import QRCodeX
from object.lnitial.lnitial_page import LnitialObject
from object.storage.packing_boxes import PackingObject
from object.storage.storage_page import StorageObject
from object.storage.unboxed_list import UnboxedObject
from case.storage.storage_page_bagmode import StorageTest
from common.request_result import GetCode
import time


# 科室装袋-不开固定箱码-未装箱列表操作
class UnboxedTest:
    def __init__(self):
        self.unit = AndroidUI
        self.home = HomePageObject()
        self.qrcode = QRCodeX()
        self.ln = LnitialObject()
        self.get_code = GetCode()
        self.driver = AndroidUI.get_webdriver()
        self.pack = PackingObject()
        # self.stobj=StorageObject()
        # self.sttest=StorageTest()
        self.un = UnboxedObject()

    # 返回
    def back_button(self):
        self.driver.keyevent(4)
        print('点击手机的返回键')

    # 判断是否登录，未登录就扫清理人二维码登录，已登录的情况下，不进行操作
    def check_login(self):
        # self.unit.scan(self.qrcode.zhaoqingli)
        try:
            self.ln.system_button()
            self.unit.scan(self.qrcode.zhaoqingli)
            print("登录成功")
        except:
            print("已经登录")

    # 判断是否在装箱界面
    def check_boxpage(self):
        try:
            self.pack.pack_box_button()
            print("已经在装箱界面了")
        except:
            self.unit.scan(self.get_code.get_bag_cod(10))
            self.pack.pack_box_button()
            print("扫码进入装箱界面成功")

    # 点击未装箱按钮进入未装箱列表
    def click_unboxed_button(self):
        try:
            self.un.unboxed_button().click()
            self.un.bags_number()
            print("点击未装箱按钮进入未装箱列表成功")
            AndroidUI().write_result("点击未装箱列表按钮进入未装箱列表", "成功")
        except:
            print("点击未装箱按钮进入未装箱列表失败")
            AndroidUI.capture()
            AndroidUI().write_result("点击未装箱列表按钮进入未装箱列表", "失败")

    # 点击左边类型第一条
    def choice_type(self, n):
        try:
            self.un.type(n).click()
            print(f"选择左边类型中第{n}个类型成功")
            AndroidUI().write_result(f"选择左边类型中第{n}个类型", "成功")
        except:
            print(f"选择左边类型中第{n}个类型失败")
            AndroidUI.capture()
            AndroidUI().write_result(f"选择左边类型中第{n}个类型", "失败")

        # self.driver.find_element_by_android_uiautomator('new UiSelector().text("感染性")').click()

        # with open(r"D:\rj自动化\GUI_Appium\date\unboxed_type.csv", encoding='utf-8') as file:
        #     content = file.readlines()
        #
        #     for i in content:
        #         type = i.strip()
        #         new = f"，{type}"
        #         # print(type)
        #         try:
        #             self.re.other_button().click()
        #             self.driver.find_element_by_android_uiautomator(f'new UiSelector().text("{type}")').click()
        #             print(f"选择类型{type}成功")
        #             time.sleep(3)
        #             self.print_bags()  # 打印
        #             # 断言，打印并添加成功
        #             try:
        #                 self.driver.find_element_by_android_uiautomator(f'new UiSelector().textContains("{new}")')
        #                 print(f"打印{type}垃圾成功")
        #                 AndroidUI().write_result(f"打印可回收垃圾{type}", "成功")
        #             except:
        #                 self.driver.find_element_by_android_uiautomator(f'new UiSelector().textContains("{new}")')
        #                 print(f"打印{type}垃圾失败")
        #                 AndroidUI.capture()
        #                 AndroidUI().write_result(f"打印可回收垃圾{type}", "成功")
        #         except:
        #             print(f"点击其他，选择{type}失败")
        # if self.un.type(1).is_enabled():
        #     print("xaunze sucess")
        # else:
        #     print('选中失败')

    # 点击第一袋垃圾袋
    def choice_bag(self, n):
        try:
            self.un.bag(n).click()
            print(f"选择列表中第{n}垃圾袋成功")
            AndroidUI().write_result(f"选择列表中第{n}垃圾袋", "成功")
        except:
            print(f"选择列表中第{n}垃圾袋失败")
            AndroidUI.capture()
            AndroidUI().write_result(f"选择列表中第{n}垃圾袋", "失败")

    # 点击全选按钮
    def choice_all(self):
        self.un.check_all_button().click()
        print("点击全选按钮成功")

    # 点击装箱按钮
    def click_box(self):

        try:
            self.un.packing_box_button().click()
            bags_number = self.un.bags_number().text
            # print(bags_number)
            if "0" in bags_number:
                print("点击装箱按钮，装箱成功,袋数为0")
                AndroidUI().write_result("点击装箱按钮装箱袋数变为0", "成功")
            else:
                print("点击装箱按钮，装箱失败test1")
                AndroidUI.capture()
                AndroidUI().write_result("点击装箱按钮装箱", "失败")
        except:
            print("点击装箱按钮，装箱失败test2")
            AndroidUI.capture()
            AndroidUI().write_result("点击装箱按钮装箱", "失败")

    def start_test(self):
        self.check_login()
        self.check_boxpage()
        self.click_unboxed_button()
        self.choice_type(1)
        # self.choice_bag(1)
        self.choice_all()
        self.click_box()  # 装箱按钮
        self.back_button()
        time.sleep(2)
        self.back_button()


if __name__ == '__main__':
    test = UnboxedTest()
    test.start_test()
